﻿using LongGi.TingkatKebocoranModule.CommModel;
using LPLS.DBEntities.LongGIModule;
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Series;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace LongGi.TingkatKebocoranModule.ViewModels
{


    /// <summary>
    /// 漏率 模型
    /// </summary>
    public class TKChartViewModel : MyViewModelBase
    {

        public TKChartViewModel()
        {
            //  LongGiEventCommon.DataEvent += LongGiEventCommon_DataEvent;

            LongGiEventCommon.LouLvDataEvent += LongGiEventCommon_LouLvDataEvent;
            LongGiEventCommon.ExportExcelDataEvent += LongGiEventCommon_ExportExcelDataEvent;
        }
        /// <summary>
        /// 数据导出 事件
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void LongGiEventCommon_ExportExcelDataEvent()
        {
            ExportFileCommon.ExportFile<LoulvData>("漏率数据", ExportData);
        }

        private List<LoulvData> ExportData = new List<LoulvData>();
        private void LongGiEventCommon_LouLvDataEvent(List<LPLS.DBEntities.LongGIModule.LoulvData> obj)
        {
            ExportData.Clear();
            var li = obj.OrderBy(e => e.Time).ToList();
            ExportData = li;
            Query(li);
        }

        private void Query(List<LoulvData> li)
        {

            Thread t = new Thread(() =>
            {


                List<MyPoint> Poins = new List<MyPoint>();


                for (int i = 0; i < li.Count; i++)
                {
                    MyPoint p = new MyPoint();

                    ExcelReader reader = new ExcelReader();
                    li[i].Value = li[i].Value;
                    p.Value = li[i].Value;
                    p.Time = li[i].Time;
                    Poins.Add(p);

                }

                var points = Poins.Where(e => e != null).ToList();
                this.LeakRatePlotModel = QueryCompentTimeUtilizationRateByTimeToLineBar(points, "漏率统计");


            });
            t.IsBackground = true;
            t.Start();
        }

  




        private PlotModel QueryCompentTimeUtilizationRateByTimeToLineBar(List<MyPoint> data, string title)
        {
            var series = new List<LineSeries>();
            var model = new PlotModel();

            var dt = data.OrderBy(e => e.Time).ToList();


            var li = new List<MyPoint>();

            foreach (var item in dt)
            {

                MyPoint m = new MyPoint();
                m.Time = item.Time; ;
                m.Value = item.Value;
                li.Add(m);
            }
            var myseries = new LineSeries
            {
                Title = title,
                ItemsSource = li,
                DataFieldX = "Time",
                DataFieldY = "Value",
                MarkerType = MarkerType.Circle,
                TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n漏率值：{4:0.00}",

            };
            series.Add(myseries);

            foreach (var item in series)
            {
                model.Series.Add(item);
            }
          
            var dateTimeAxis = new DateTimeAxis
            {
                Position = AxisPosition.Bottom,
                IntervalType = DateTimeIntervalType.Auto,
            };
            model.Axes.Add(dateTimeAxis);
            var valueAxis = new LinearAxis
            {


                Position = AxisPosition.Left,
                Minimum = -1,
                Maximum = 5,
                Title = "漏率",
                IsPanEnabled = false,
                IsZoomEnabled = false

            };


            model.Axes.Add(valueAxis);
            Legend l = new Legend();
            l = LongGiCommon.GetLegend();
            model.Legends.Add(l);
            return model;

        }



 

     


     

    }











}
